// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
//人脸检测
//需要自己安装文件依赖npm install tencentcloud-sdk-nodejs --save
//npm install wx-server-sdk --save
//node index.js
const tencentcloud = require("tencentcloud-sdk-nodejs");
  
function syncFaceDetect(base64Img){
    const IaiClient = tencentcloud.iai.v20180301.Client;
    const models = tencentcloud.iai.v20180301.Models;

    const Credential = tencentcloud.common.Credential;
    const ClientProfile = tencentcloud.common.ClientProfile;
    const HttpProfile = tencentcloud.common.HttpProfile;

    let cred = new Credential("AKIDPxHLGwWostHUHohCbqundNs0deS6Ziqe", "vZ0jE4QBkCUccLtEKFxgmz0oNhogJm02");
    let httpProfile = new HttpProfile();
    httpProfile.endpoint = "iai.tencentcloudapi.com";
    let clientProfile = new ClientProfile();
    clientProfile.httpProfile = httpProfile;
    let client = new IaiClient(cred, "ap-guangzhou", clientProfile);

    let req = new models.DetectFaceRequest();

    let params = '{"Image":"'+base64Img+'","NeedFaceAttributes":1}'
    req.from_json_string(params);

  return new Promise((resolve,reject)=>{
    client.DetectFace(req, function (errMsg, response) {
      if (errMsg) {
        reject(errMsg);
        return;
      }
      resolve(response);
    });
  })
}
// 云函数入口函数
exports.main = async (event, context) => {
  let {base64Img} = event;
  let res = await syncFaceDetect(base64Img);

  return {
    res
  }
}